import { coptcSelectCoptd } from '@/api/url/salesOrderManagement';
import router from '@/router';
import { categoriesTaxesList } from '@/utils/base';
import { EFTimeFormatConversion } from '@/utils/baseMethods';
import { reactive } from 'vue';
import { completeTask } from '@api/url/processManagement';
import notification from '@arco-design/web-vue/es/notification';

export default function importConfig() {
  const base = reactive<any>({
    headerData: null,
    bottomData: null,
    tabelData: null,
    dataOne: null,
    dataTwo: null,
    from: null,
  });

  const init = () => {
    getTable();
  };

  const getTable = () => {
    if (router.currentRoute.value.query.db) {
      base.from = 'sale';
      const steam = {
        db: router.currentRoute.value.query.db,
        dh: router.currentRoute.value.query.dh,
      };
      coptcSelectCoptd(steam).then((res: any) => {
        if (res.code == 200) {
          console.log(res);
          base.tabelData = res.data;
          headerDataOption();
          bottomDataOption();
        }
      });
    } else {
      base.from = 'task';
      const res = JSON.parse(localStorage.getItem('currentUserTaskData'));
      base.tabelData = res.data;
      headerDataOption();
      bottomDataOption();
    }
  };

  /**
   * @exports headerDataOption 表头数据载入
   */
  const headerDataOption = () => {
    base.dataOne = [
      {
        label: '订单单号',
        value: base.tabelData.coptc?.tc001 + '-' + base.tabelData.coptc?.tc002,
      },
      {
        label: '订单日期',
        value: EFTimeFormatConversion(base.tabelData.coptc?.tc003),
      },
      {
        label: '部门编号',
        value: base.tabelData.coptc?.tc005,
      },
      {
        label: '业务人员',
        value: base.tabelData.coptc?.tc006,
      },
      {
        label: '出货工厂',
        value: base.tabelData.coptc?.tc007,
      },
      {
        label: '客户合同交期',
        value: EFTimeFormatConversion(base.tabelData.coptc?.tci01),
      },
      {
        label: '客户订单日期',
        value: EFTimeFormatConversion(base.tabelData.coptc?.tci02),
      },
      {
        label: '单据日期',
        value: EFTimeFormatConversion(base.tabelData.coptc?.tc039),
      },
      {
        label: '交易条件',
        value: base.tabelData.coptc?.udf02,
      },
      {
        label: '付款条件',
        value: base.tabelData.coptc?.tc014,
      },

      {
        label: '运输方式',
        value: base.tabelData.coptc?.tc019,
      },
      {
        label: '备注',
        value: base.tabelData.coptc?.remark,
      },
    ];
    base.dataTwo = [
      {
        label: '客户编号',
        value: base.tabelData.coptc?.tc004,
      },
      {
        label: '客户单号',
        value: base.tabelData.coptc?.tc012,
      },
      {
        label: '供应链采购单号',
        value: base.tabelData.coptc?.udf01,
      },
      {
        label: '送货地址',
        value: base.tabelData.coptc?.tc010,
      },
      // {
      //   label: '送货地址(二)',
      //   value: base.tabelData.coptc?.tc011,
      // },

      {
        label: '收货客户编号',
        value: base.tabelData.coptc?.udf06,
      },
      {
        label: '收货客户',
        value: base.tabelData.coptc?.udf03,
      },
      {
        label: '联系人',
        value: base.tabelData.coptc?.tc018,
      },
      {
        label: '电话',
        value: base.tabelData.coptc?.tc074,
      },
      {
        label: '传真',
        value: base.tabelData.coptc?.tc075,
      },
      {
        label: '收货客户地址',
        value: base.tabelData.coptc?.udf05,
      },
      {
        label: '客户业务联络人',
        value: base.tabelData.coptc?.udf04,
      },
    ];
    base.headerData = [
      {
        label: '订单金额',
        value: base.tabelData.coptc?.tc029,
      },
      {
        label: '订单税额',
        value: base.tabelData.coptc?.tc030,
      },
      {
        label: '总数量',
        value: base.tabelData.coptc?.tc031,
      },

      {
        label: '税率',
        value: base.tabelData.coptc?.tc041 * 100 + '%',
      },
      {
        label: '交易币种',
        value: base.tabelData.coptc?.tc008,
      },
      {
        label: '汇率',
        value: base.tabelData.coptc?.tc009,
      },
      {
        label: '价格说明',
        value: base.tabelData.coptc?.tc013,
      },
      {
        label: '税种',
        value: categoriesTaxesList(base.tabelData.coptc?.tc016).label,
      },
      {
        label: '税号',
        value: base.tabelData.coptc?.tc073,
      },
      {
        label: '审核者',
        value: base.tabelData.coptc?.tc040,
      },
    ];
  };

  /**
   * @exports bottomDataOption 单身数据载入
   */
  const bottomDataOption = () => {
    base.bottomData = [];
    base.tabelData.coptdList.map((res: any) => {
      const steam = [
        {
          label: '单别:',
          value: res?.td001,
        },
        {
          label: '单号:',
          value: res?.td002,
        },
        {
          label: '序号:',
          value: res?.td003,
        },
        {
          label: '品号:',
          value: res?.td004,
        },
        {
          label: '品名:',
          value: res?.td005,
        },
        {
          label: '规格:',
          value: res?.td006,
        },
        {
          label: '仓库:',
          value: res?.td007,
        },

        {
          label: '已交数量:',
          value: res?.td009,
        },
        {
          label: '订单数量:',
          value: res?.td008,
        },

        {
          label: '单价:',
          value: res?.td011,
        },
        {
          label: '单位:',
          value: res?.td010,
        },
        {
          label: '金额:',
          value: res?.td012,
        },
        {
          label: '税率:',
          value: res?.td037,
        },
        {
          label: '税前金额:',
          value: res?.td038,
        },
        {
          label: '税额:',
          value: res?.td039,
        },
        {
          label: '预交货日:',
          value: EFTimeFormatConversion(res?.td013),
        },
        {
          label: '生管确认日:',
          value: EFTimeFormatConversion(res?.udf03),
        },
        {
          label: '客户品号:',
          value: res?.td014,
        },
        {
          label: '预测编号:',
          value: res?.td015,
        },
        {
          label: '结束:',
          value: res?.td016,
        },
        {
          label: '前置单据-单别:',
          value: res?.td017,
        },
        {
          label: '前置单据-单号:',
          value: res?.td018,
        },
        {
          label: '前置单据-序号:',
          value: res?.td019,
        },
        {
          label: '备注:',
          value: res?.td020,
        },
        {
          label: '审核码:',
          value: res?.td021,
        },
        {
          label: '库存数量:',
          value: res?.td022,
        },
        {
          label: '小单位:',
          value: res?.td023,
        },
        {
          label: '赠品量:',
          value: res?.td024,
        },
        {
          label: '赠品已交量:',
          value: res?.td025,
        },
        {
          label: '折扣率:',
          value: res?.td026,
        },
        {
          label: '项目编号:',
          value: res?.td027,
        },
        {
          label: '预测序号:',
          value: res?.td028,
        },
        {
          label: '包装方式:',
          value: res?.td029,
        },
        {
          label: '毛重(Kg):',
          value: res?.td030,
        },
        {
          label: '材积(CUFT):',
          value: res?.td031,
        },
        {
          label: '订单包装数量:',
          value: res?.td032,
        },
        {
          label: '已交包装数量:',
          value: res?.td033,
        },
        {
          label: '赠品包装量:',
          value: res?.td034,
        },
        {
          label: '赠品已交包装量:',
          value: res?.td035,
        },
        {
          label: '包装单位:',
          value: res?.td036,
        },

        {
          label: '件装:',
          value: res?.td040,
        },
        {
          label: '件数:',
          value: res?.td041,
        },
        {
          label: '批发价:',
          value: res?.td042,
        },
        {
          label: '零售价:',
          value: res?.td043,
        },
        {
          label: '出货待销量:',
          value: res?.td044,
        },
      ];
      base.bottomData.push(steam);
    });
    console.log(base.bottomData);
  };

  /**
   * @exports arrayFinally 是否为最后的数据进行判断
   */
  const arrayFinally = (index) => {
    let steam = false;
    if (base.bottomData.length - 1 == index && index % 2 == 0) {
      steam = true;
    }
    return steam;
  };

  //是否同意完成
  const complete = (remark) => {
    const obj = {
      remark: remark,
      taskID: base.taskID,
      userName: localStorage.getItem('userName'),
    };
    completeTask(obj).then((res) => {
      if (res.code == 200) {
        notification.info('操作成功');
        router.go(-1);
      } else {
        notification.info('失败');
      }
    });
  };

  //当前用户任务同意
  const agree = () => {
    complete('同意');
  };
  //当前用户任务不同意
  const disagree = () => {
    complete('不同意');
  };

  return {
    base,
    init,
    headerDataOption,
    bottomDataOption,
    arrayFinally,
    agree,
    disagree,
  };
}
